/**
 * 侧边栏动画与音效封装
 */
class SidebarAnimation {
    constructor(options = {}) {
        this.options = {
            soundEnabled: true,
            clickSoundUrl: 'https://assets.mixkit.co/sfx/preview/mixkit-arcade-game-jump-coin-216.mp3',
            hoverSoundUrl: 'https://assets.mixkit.co/sfx/preview/mixkit-casino-slot-machine-win-1046.mp3',
            ...options
        };
        
        this.initSounds();
        this.bindEvents();
    }
    
    // 初始化音效
    initSounds() {
        if (!this.options.soundEnabled) return;
        
        this.clickSound = new Audio(this.options.clickSoundUrl);
        this.hoverSound = new Audio(this.options.hoverSoundUrl);
        
        // 设置音量
        this.clickSound.volume = 0.3;
        this.hoverSound.volume = 0.2;
    }
    
    // 播放点击音效
    playClickSound() {
        if (!this.options.soundEnabled || !this.clickSound) return;
        
        this.clickSound.currentTime = 0;
        this.clickSound.play().catch(err => console.log('Audio play error:', err));
    }
    
    // 播放悬停音效
    playHoverSound() {
        if (!this.options.soundEnabled || !this.hoverSound) return;
        
        this.hoverSound.currentTime = 0;
        this.hoverSound.play().catch(err => console.log('Audio play error:', err));
    }
    
    // 绑定事件监听
    bindEvents() {
        // 菜单项悬停事件
        document.querySelectorAll('.menu-item').forEach(item => {
            item.addEventListener('mouseenter', () => {
                this.playHoverSound();
            });
        });
        
        // 菜单项点击事件
        document.querySelectorAll('.menu-item').forEach(item => {
            item.addEventListener('click', () => {
                this.playClickSound();
                this.addPulseEffect(item);
            });
        });
        
        // 子菜单展开/收起事件
        document.querySelectorAll('.el-sub-menu').forEach(submenu => {
            const title = submenu.querySelector('.submenu-title');
            if (title) {
                title.addEventListener('click', () => {
                    const submenuContent = submenu.querySelector('.el-sub-menu__content');
                    if (submenuContent) {
                        if (submenuContent.style.display === 'none') {
                            this.expandSubmenu(submenuContent);
                        } else {
                            this.collapseSubmenu(submenuContent);
                        }
                    }
                });
            }
        });
    }
    
    // 添加脉冲效果
    addPulseEffect(element) {
        element.classList.add('pulse-effect');
        setTimeout(() => {
            element.classList.remove('pulse-effect');
        }, 500);
    }
    
    // 展开子菜单
    expandSubmenu(element) {
        element.style.display = 'block';
        element.classList.add('menu-expand');
        setTimeout(() => {
            element.classList.remove('menu-expand');
        }, 300);
    }
    
    // 收起子菜单
    collapseSubmenu(element) {
        element.classList.add('menu-collapse');
        setTimeout(() => {
            element.style.display = 'none';
            element.classList.remove('menu-collapse');
        }, 300);
    }
    
    // 初始化logo动画
    initLogoAnimation() {
        const logo = document.querySelector('.logo-glow');
        if (logo) {
            logo.style.animation = 'glow 2s ease-in-out infinite alternate';
        }
    }
}

// 初始化侧边栏动画
window.addEventListener('DOMContentLoaded', () => {
    const sidebarAnimation = new SidebarAnimation({
        soundEnabled: true
    });
    
    sidebarAnimation.initLogoAnimation();
});